package com.example.hzz.criminalintent;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

import java.util.ArrayList;
import java.util.UUID;

/**
 * Created by huangzezhan on 2018/9/2.
 */

public class CrimePageActivity extends FragmentActivity {
    private static final String TAG = "CrimePageActivity";
    private ViewPager mViewPager;
    private ArrayList<Crime> mCrimes;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        mViewPager = new ViewPager(this);
//        mViewPager.setId(R.id.viewpager);
//        setContentView(mViewPager);
        setContentView(R.layout.activity_pager_layout);
        mViewPager = (ViewPager) findViewById(R.id.viewPagerid);
        Log.i(TAG, "onCreate mViewPager=" + mViewPager);

        mCrimes = CrimeLab.getInstance(this).getCrimes();
        FragmentManager fm = getSupportFragmentManager();

        mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {
            @Override
            public Fragment getItem(int position) {
                Crime crime = mCrimes.get(position);
                return CrimeViewFragment.newInstance(crime.getmId());
            }

            @Override
            public int getCount() {
                return  mCrimes.size();
            }
        });

        UUID crimeId = (UUID) getIntent().getSerializableExtra("CRIME_ID");
        for (int i = 0; i < mCrimes.size(); i++) {
            if (mCrimes.get(i).getmId().equals(crimeId)) {
                mViewPager.setCurrentItem(i);
            }
        }
    }
}
